<template>
  <div class="login">
    <div class="left-box">
      <img class="bg_img" src="@/assets/images/camp/left_banner.png">
      <img class="pic_img" src="@/assets/images/camp/banner-img@2x.png"/>
    </div>

    <div class="right-box">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">账号密码登录</h3>
        <div class="line"></div>
        <el-form-item prop="username">
          <img class="input-icon" src="@/assets/images/camp/username.png"/>
          <el-input v-model="loginForm.username" type="text" autocomplete="off" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <img class="input-icon" src="@/assets/images/camp/password.png"/>
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            autocomplete="off"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code" class="code">
          <img class="input-icon" src="@/assets/images/camp/code.png"/>
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            @keyup.enter.native="handleLogin"
          ></el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <!--      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>-->
    </div>
  </div>
</template>

<script>
  import {getCodeImg} from "@/api/login";
  import Cookies from "js-cookie";
  import {encrypt, decrypt} from '@/utils/jsencrypt'

  export default {
    name: "login_camp",
    data() {
      return {
        codeUrl: "",
        cookiePassword: "",
        loginForm: {
          username: "",
          password: "",
          rememberMe: false,
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [
            {required: true, trigger: "blur", message: "用户名不能为空"}
          ],
          password: [
            {required: true, trigger: "blur", message: "密码不能为空"}
          ],
          code: [{required: true, trigger: "change", message: "验证码不能为空"}]
        },
        loading: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode();
      this.getCookie();
    },
    methods: {
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {expires: 30});
              Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
              Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({path: this.redirect || "/"}).catch(() => {
              });
            }).catch(() => {
              this.loading = false;
              this.getCode();
            });
          }
        });
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    width: 100%;
    min-height: 100%;
    min-width: 960px;
    background-color: #F8F8F8;
    display: flex;
    flex-flow: row;

    .left-box {
      position: relative;
      width: 26%;
      min-height: 100%;
      max-width: 500px;
      min-width: 350px;
      background-color: #3E67E9;

      .bg_img {
        width: 100%;
      }


      .pic_img {
        width: 83%;
        position: absolute;
        left: 50%;
        top: 55%;
        transform: translate(-50%, 0);
      }
    }

    .right-box {
      flex: 1;
      position: relative;


      .title {
        height: 34px;
        line-height: 34px;
        font-size: 18px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #4D75F3;
        margin: 0;
      }

      .line {
        width: 2.4em;
        height: 2px;
        background-color: #4D75F3;
        border-radius: 2px;
        margin-bottom: 30px;
      }

      .login-form {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: #ffffff;
        width: 36%;
        min-width: 370px;
        max-width: 520px;
        padding: 4% 4% 6.5%;
        box-shadow: 0px 1px 18px 0px rgba(0, 0, 0, 0.06);
        border-radius: 8px;

        .el-form-item {
          margin-bottom: 17px;

          .el-form-item__content {
            position: relative;

            .el-input {
              height: 42px;

              .el-input__inner {
                height: 42px;
                border: 0;
                border-radius: 0;
                border-bottom: 1px solid #ECECEC;
                padding: 0 15px 0 38px;
                line-height: 42px;

                &:hover, &:focus {
                  border-color: #4D75F3;
                }
              }
            }

            .input-icon {
              width: 30px;
              position: absolute;
              left: 0;
              top: 50%;
              transform: translate(0, -50%);
              z-index: 10;
            }

            .login-code {
              position: absolute;
              right: 0;
              top: 0;
              height: 42px;
              .login-code-img {
                height: 38px;
                cursor: pointer;
                vertical-align: middle;
                margin-top: 1px;
              }
            }

            .el-button {
              height: 42px;
              background-color: #4D75F3;
              border-radius: 4px;
              opacity: 0.8;
            }
          }
          &.code{
            .el-input__inner {
              padding-right: 35% !important;
            }
          }
        }
      }


      .el-login-footer {
        height: 40px;
        line-height: 40px;
        position: fixed;
        bottom: 0;
        width: 100%;
        text-align: center;
        color: #fff;
        font-family: Arial;
        font-size: 12px;
        letter-spacing: 1px;
      }

    }
  }
</style>
